#!/usr/bin/python -S
"""
jsonpattern_unittest.py
"""

__author__ = 'Andy Chu'


import re
import sys
import unittest
import pprint

import _jsonpattern as jsonpattern
import _exceptions
import parse


class MatchTest(unittest.TestCase):

  def testMatch(self):
    # These were useful for starting development, but might not need them
    return
    fragments = ['a', '[A-Z]', 'b']
    fragments = [re.compile(f) for f in fragments]

    result = jsonpattern.Match(fragments, 'aLb')
    self.assertEqual(
        result, ({}, 3))

    self.assertEqual(
        jsonpattern.Match(fragments, 'NOMATCH'),
        (None, 0))

    fragments.append(
        (jsonpattern._DoNamedCapture, ('digits', re.compile(r'\d+')))
        )

    fragments.append(
        (jsonpattern._DoNamedCapture, ('alpha', re.compile(r'[a-z]*')))
        )

    self.assertEqual(
        jsonpattern.Match(fragments, 'aLb35'),
        ({'digits': '35', 'alpha': ''}, 5))

    self.assertEqual(
        jsonpattern.Match(fragments, 'aLb35xy'),
        ({'digits': '35', 'alpha': 'xy'}, 7))

    section = parse._Section(section_name='nums')
    section.fragments = {'default': [
        (jsonpattern._DoNamedCapture, ('@', re.compile(r'\d+'))),
        re.compile(',?'),
        ]}
    fragments.append((jsonpattern._DoRepeatedCapture, section))

    print fragments
    print jsonpattern.Match(fragments, 'aLb35xy1,23,456,7,8,')

  def testRepeatedmatch(self):
    # These were useful for starting development, but might not need them
    return
    section = parse._Section(section_name='nums')
    section.fragments = {'default': [
        (jsonpattern._DoNamedCapture, ('@', re.compile(r'\d+'))),
        re.compile(',?'),
        ]}
    fragments = [(jsonpattern._DoRepeatedCapture, section)]
    print jsonpattern.Match(fragments, '1,23,456,7,8,')

    section = parse._Section(section_name='pairs')
    section.fragments = {'default': [
        (jsonpattern._DoNamedCapture, ('num', re.compile(r'\d+'))),
        (jsonpattern._DoNamedCapture, ('chars', re.compile(r'\w+'))),
        re.compile(',?'),
        ]}
    fragments = [(jsonpattern._DoRepeatedCapture, section)]
    print jsonpattern.Match(fragments, '1a,23xy,456dd,7ee,8a,')


if __name__ == '__main__':
  unittest.main()
